package jp.ac.u_tokyo.dch.action.schema;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import jp.ac.u_tokyo.dch.core.db.DBConnection;
import jp.ac.u_tokyo.dch.dao.common.SeqenceGenerator;
import jp.ac.u_tokyo.dch.model.db.DataSchema;
import jp.ac.u_tokyo.dch.model.db.DataSchemaDetail;
import jp.ac.u_tokyo.dch.model.xml.DisplayDefine;
import jp.ac.u_tokyo.dch.service.DaoServiceAware;
import jp.ac.u_tokyo.dch.service.DataSchemaService;
import jp.ac.u_tokyo.dch.service.Transaction;
import jp.ac.u_tokyo.dch.util.DchCommonUtil;
import jp.ac.u_tokyo.dch.util.common.ModoruUtil;
import jp.ac.u_tokyo.dch.util.common.CommonUtil;
import jp.ac.u_tokyo.dch.util.common.DisplayUtil;
import jp.ac.u_tokyo.dch.view.Constants;
import jp.ac.u_tokyo.dch.view.outputschema.SchemaView;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
*
* @version 1.1
*
*/
public class SchemaManageAction extends ActionSupport implements ModelDriven<SchemaView>,DaoServiceAware {

	/**
	 *シリアル番号
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 画面モデル
	 */
	private SchemaView view = new SchemaView();

	/**
	 * 画面定義
	 */
	private DisplayDefine define;

	/**
	 * データスキーマリスト
	 */
	private List<DataSchema> dataSchemaList;

	/**
	 * 実行タイプ
	 */
	private String actionType;

	/**
	 * DBコネクションオブジェクト
	 */
	private DBConnection dbConnection = null;

	/**
	 * サービス
	 */
	private DataSchemaService service = null;


	/**
	 * 初期化
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String init() throws Exception {
		return SUCCESS;
	}
	/**
	 * リスト表示
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String list() throws Exception {

		ModoruUtil.modoruProcess(ServletActionContext.getRequest(), "/outputschema/manage-list", ServletActionContext.getRequest().getQueryString());

		service = new DataSchemaService(dbConnection);

		String sort =  view.getSort();

		dataSchemaList = service.getList(sort);

		return SUCCESS;
	}
	/**
	 * 入力タイプ編集
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String editInputType() throws Exception {

		define = DisplayUtil.getItemDefine(view.getSort());

		view.editInputFormat();
		return SUCCESS;
	}
	/**
	 * 登録
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String add() throws Exception {
		actionType = Constants.ADD;

		define = DisplayUtil.getItemDefine(view.getSort());

		String fUserId = DchCommonUtil.getLoginIdFromRequest(ServletActionContext.getRequest());
		view.getDataSchema().setSort(view.getSort());
		view.getDataSchema().setCreateUser(fUserId);
		view.getDataSchema().setCreateDate(DchCommonUtil.getCurrentDate());
		view.getDataSchema().setModifyUser(fUserId);
		view.getDataSchema().setModifyDate(DchCommonUtil.getCurrentDate());

		return SUCCESS;
	}
	/**
	 * 確認画面から戻る処理
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String modoru() throws Exception {

		define = DisplayUtil.getItemDefine(view.getSort());

		return SUCCESS;
	}

	/**
	 * カスタムスキーマ項目追加
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String addCustomSchema() throws Exception {

		define = DisplayUtil.getItemDefine(view.getSort());

		SeqenceGenerator generator = new SeqenceGenerator(dbConnection);

		view.addCustomSchema(generator.generateFieldKey());
		return SUCCESS;
	}
	/**
	 * カスタムスキーマ項目編集
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String editCustomSchema() throws Exception {

		define = DisplayUtil.getItemDefine(view.getSort());

		view.editCustomSchema();
		return SUCCESS;
	}
	/**
	 * カスタムスキーマ項目削除
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String deleteCustomSchema() throws Exception {

		define = DisplayUtil.getItemDefine(view.getSort());

		view.deleteCustomSchema();
		return SUCCESS;
	}
	/**
	 * 親スキーマ選択
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String selectOyaSchema() throws Exception {

		define = DisplayUtil.getItemDefine(view.getSort());

		String oyaSchemaId = view.getSelectOyaid();

		service = new DataSchemaService(dbConnection);

		DataSchema dataSchema = service.findSchema(oyaSchemaId);

		view.getDataSchema().setOyaschemaid(oyaSchemaId);
		view.getDataSchema().setOyaschemaName(dataSchema.getName());

		return SUCCESS;
	}
	/**
	 * 確認する
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String confirm() throws Exception {

		define = DisplayUtil.getItemDefine(view.getSort());

		String schemaName = view.getDataSchema().getName();
		String schemaid = view.getDataSchema().getSchemaid();
		String sort = view.getDataSchema().getSort();
		String oyaschemaid = view.getDataSchema().getOyaschemaid();

		service = new DataSchemaService(dbConnection);

		if (CommonUtil.oyaEmpty(sort, oyaschemaid)) {
			addFieldError("id", getText("requiredstring", new String[] { "親スキーマ" }));
			return ERROR;
		}

		if (StringUtils.isEmpty(schemaName)) {
			addFieldError("dataSchema.name", getText("requiredstring", new String[] { "メタデータスキーマ名称" }));
			return ERROR;
		}

		if (StringUtils.contains(schemaName, "\"")) {
			addFieldError("id", getText("escapeerror", new String[] { "メタデータスキーマ名称" }));
			return ERROR;
		}

		if (service.checkName(sort, schemaName, schemaid)) {
			addFieldError("dataSchema.name", getText("duplicateMsg"));
			return ERROR;

		}

		return SUCCESS;
	}
	/**
	 * 編集処理
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String editInit() throws Exception {

		actionType = Constants.MOD;

		define = DisplayUtil.getItemDefine(view.getSort());

		HttpServletRequest request = ServletActionContext.getRequest();
		String schemaid = request.getParameter("schemaid");
		String sort = request.getParameter("sort");

		view.setSort(sort);

		service = new DataSchemaService(dbConnection);

		service.setOutputSchemaView(view, schemaid);

		String fUserId = DchCommonUtil.getLoginIdFromRequest(ServletActionContext.getRequest());
		view.getDataSchema().setModifyUser(fUserId);
		view.getDataSchema().setModifyDate(DchCommonUtil.getCurrentDate());

		return SUCCESS;
	}
	/**
	 * コピー処理
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String copyInit() throws Exception {


		actionType = Constants.ADD;

		define = DisplayUtil.getItemDefine(view.getSort());

		HttpServletRequest request = ServletActionContext.getRequest();
		String schemaid = request.getParameter("schemaid");
		String sort = request.getParameter("sort");

		view.setSort(sort);

		service = new DataSchemaService(dbConnection);

		service.setOutputSchemaView(view, schemaid);

		SeqenceGenerator seqenceGenerator = new SeqenceGenerator(dbConnection);
		//項目キー生成
		for (DataSchemaDetail detail : view.getCustomItemList()) {

			detail.setFieldKey(seqenceGenerator.generateFieldKey());
		}

		String name = view.getDataSchema().getName();
		view.getDataSchema().setName("コピー " + name);

		String fUserId = DchCommonUtil.getLoginIdFromRequest(ServletActionContext.getRequest());
		view.getDataSchema().setSort(view.getSort());
		view.getDataSchema().setCreateUser(fUserId);
		view.getDataSchema().setModifyUser(fUserId);
		view.getDataSchema().setModifyDate(DchCommonUtil.getCurrentDate());

		return SUCCESS;

	}
	/**
	 * 削除処理
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String delInit() throws Exception {

		actionType = Constants.DEL;

		define = DisplayUtil.getItemDefine(view.getSort());

		HttpServletRequest request = ServletActionContext.getRequest();
		String schemaid = request.getParameter("schemaid");
		String sort = request.getParameter("sort");

		view.setSort(sort);

		service = new DataSchemaService(dbConnection);

		service.setOutputSchemaView(view, schemaid);

		return SUCCESS;
	}
	/**
	 * 登録する
	 * @return
	 * @throws Exception
	 */
	@Transaction
	public String execute() throws Exception {

		service = new DataSchemaService(dbConnection);

		if (Constants.ADD.equals(actionType)) {

			service.insert(view);

		} else if (Constants.MOD.equals(actionType)) {

			service.update(view);

		} else if (Constants.DEL.equals(actionType)) {

			service.delete(view);
		}

		return SUCCESS;
	}

 	/**
	 *
	 * @return スキーマリスト
	 */
	public List<DataSchema> getDataSchemaList() {
		return dataSchemaList;
	}

	/**
	 *
	 * @return
	 */
	public DisplayDefine getDefine() {
		return define;
	}
	/**
	 *
	 * @param fieldkey 項目キー
	 * @return スキーマ詳細情報
	 */
	public DataSchemaDetail getDataSchemaDetailByFieldKey(String fieldkey) {

		DataSchemaDetail detail = view.getDataSchemaDetailMap().get(fieldkey);

		if (detail == null) {
			detail = new DataSchemaDetail();
			detail.setFieldKey(fieldkey);
			detail.setInputType("0");
			detail.setRequired("0");
			detail.setDisplay("0");
			detail.setHissuSelectSign("0");

		}

		return detail;
	}

	@Override
	public SchemaView getModel() {
		return view;
	}


	@Override
	public void setDBConnection(DBConnection con) {
		dbConnection = con;

	}
	/**
	 * 画面名を取得する
	 * @return 画面名
	 */
	public String getGamenName() {

		return getText("outputmenu_" + view.getSort());
	}

	/**
	 * 親画面名を取得する
	 * @return 親画面名
	 */
	public String getGamenOyaName() {

		return getText("outputmenu_" + view.getOyaSort());
	}

//	/**
//	 * 順番によって項目キーを生成
//	 * @param 順番
//	 * @return 項目キー
//	 */
//	public String getFieldKey(String index) {
//
//		return OutputCommonUtil.getFieldKey(view.getSort(), index);
//
//	}

	/**
	 *
	 * @return 実行タイプ
	 */
	public String getActionType() {
		return actionType;
	}
	/**
	 *
	 * @param 実行タイプ
	 */
	public void setActionType(String actionType) {
		this.actionType = actionType;
	}


}
